/*
 *
 *  * Copyright 2022  腾羚智能, Inc. All rights reserved.
 *
 */

package com.ruoyi.framework.config;

import com.baomidou.mybatisplus.autoconfigure.MybatisPlusProperties;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.core.handlers.MybatisEnumTypeHandler;
import com.baomidou.mybatisplus.extension.MybatisMapWrapperFactory;
import com.ruoyi.framework.config.properties.MybatisPlusAutoFillProperties;
import com.ruoyi.framework.handler.dbHandler.DateMetaObjectHandler;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;

/**
 * mybatis-plus自动配置
 *
 *
 *
 * <p>
 */
@EnableConfigurationProperties(MybatisPlusAutoFillProperties.class)
public class MybatisPlusAutoConfigure {



    @Autowired
    private MybatisPlusAutoFillProperties autoFillProperties;

    @Resource
    private MybatisPlusProperties properties;

    @PostConstruct
    public void addInterceptor() {
        if (this.properties.getConfiguration() != null) {
            //resultType=“java.util.Map“时Map下划线自动转驼峰
            this.properties.getConfiguration().setObjectWrapperFactory(new MybatisMapWrapperFactory());
            this.properties.getConfiguration().setDefaultEnumTypeHandler(MybatisEnumTypeHandler.class);
        }
    }

    @Bean
    @ConditionalOnMissingBean
    @ConditionalOnProperty(prefix = "mediation.mybatis-plus.auto-fill", name = "enabled", havingValue = "true", matchIfMissing = true)
    public MetaObjectHandler metaObjectHandler() {
        return new DateMetaObjectHandler(this.autoFillProperties);
    }
}
